//
//  JMTextField.m
//  NbTest
//
//  Created by admin on 12-3-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JMTextField.h"
#import "JMEnv.h"
#define JM_Return_Btn_Height 40
@interface JMTextField ()
{
    UIButton*   _ButtonNext;
    UIButton*   _ButtonComplete;
    JMTextFildDelegateDefault* _defaultDelegate;
    JMTextField* _nextEditer;
    int             _offSetOld;
}
- (void) setMoneyType;
- (void) setNumberType;
- (void) setPinYinType;

- (void) returnText: (id)sender;
- (void) nextClick: (id)sender;
@end

@implementation JMTextField
@synthesize _defaultDelegate;
//@synthesize _nextEditer;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        // Initialization code
        
        self.textColor = ENV.iTextColor;
        self.borderStyle = UITextBorderStyleNone;//UITextBorderStyleRoundedRect;//设置文本框边框风格
        
        self.autocorrectionType = UITextAutocorrectionTypeYes;//启用自动提示更正功能
        
        self.placeholder = @"Hello, I am textField";//设置默认显示文本
        
        self.returnKeyType = UIReturnKeyDone;//设置键盘完成按钮，相应的还有“Return”"Gｏ""Google"等
        
        self.clearButtonMode = UITextFieldViewModeWhileEditing;

        self.canCopyAndPaste = YES;
        JMTextFildDelegateDefault * defaultDelegat = [[JMTextFildDelegateDefault alloc] init];
        
        
        self._defaultDelegate = defaultDelegat;
        
        self.delegate = _defaultDelegate;
        
        [defaultDelegat release];

       // [self setMoneyType];

        
        //加到消息队列
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self
         
                                                 selector:@selector(keyboardWasShown:)
         
                                                     name:UIKeyboardWillShowNotification object:_moveList];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
         
                                                 selector:@selector(keyboardWasHidden:)
         
                                                     name:UIKeyboardWillHideNotification object:_moveList];
        
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame WithList:(UITableView*) aTabelView;
{
    if (self = [self initWithFrame:frame])
    {
        _moveList = [aTabelView retain];
        //self.indexPath = aIndex;
        //[self setMoneyType];
    }
    
    return  self;
}

- (id)initWithType:(enum JMTextFieldType)aType WithList:(UITableView*) aTabelView
{
    if ( self = [self initWithFrame:CGRectMake(130, 10, 170, 30)]  )
    {
        _moveList = [aTabelView retain];
        switch (aType) 
        {
            case EMoneyType:
                [self setMoneyType];
                break;
            case ENumberType:
                [self setNumberType];
                break;
            case EPinYinType:
                [self setPinYinType];
                break;
            case EDynPass:
                [self setNumberType];
                _type = EDynPass;
                self.iLimitInputCount = 6;
            default:
                break;
        }
    }
    
    return  self;
}



- (void) keyboardWasShown:(NSNotification*)aNotification
{
    if ([self isFirstResponder])
    {
        if (_moveList)
        {
            _offSetOld = _moveList.contentOffset.y;
            NSDictionary* info = [aNotification userInfo];
            NSValue* value = [info objectForKey:@"UIKeyboardBoundsUserInfoKey"];
            CGSize size = [value CGRectValue].size;   
            
            UIWindow* window =  [[UIApplication sharedApplication].delegate window] ;
            CGPoint p  = [self convertPoint:self.frame.origin toView:window];
            int yEditer = p.y+self.frame.size.height;//编辑框左下角相对于屏幕的点的y坐标
            if (window.frame.origin.y!=0)
            {
                yEditer+=window.frame.origin.y;
            }
            int height = [[UIScreen mainScreen] bounds].size.height - size.height;//键盘的左上角相对于屏幕的y坐标
            int moveHeight = yEditer - height;
            if (moveHeight>0)
            {
                CGPoint scrollPoint = CGPointMake(0.0, moveHeight+_offSetOld);
            
                [_moveList setContentOffset:scrollPoint animated:YES];
            }
        }
    }
}

- (void) keyboardWasHidden:(NSNotification*) aNotification
{
    if ([self isFirstResponder])
    {
        
        if (_moveList)
        {
            CGPoint scrollPoint = CGPointMake(0.0,_offSetOld);
            
            [_moveList setContentOffset:scrollPoint animated:YES];
        }
    }
}


-(void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_defaultDelegate release];
    [_moveList release];
    
    [_ButtonNext release];
    [_ButtonComplete release];
    [_nextEditer release];
    [super dealloc];   
}

#pragma -mark 小数点键盘相关函数
- (void) setMoneyType{
	//包含小数点的数字键盘
    _type = EMoneyType;
	
	if ([[[UIDevice currentDevice] systemVersion] floatValue]<4.1) {
		self.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
	}else {
        
        UIView* uv = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,JM_Return_Btn_Height)];
        //self.inputView = nil;
        self.inputAccessoryView = uv;
        [uv release];
        
		UIButton *returnButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, JM_Return_Btn_Height)];
		[returnButton addTarget:self action:@selector(returnText:) forControlEvents:UIControlEventTouchUpInside];
        [returnButton setTitleColor:[UIColor blackColor] forState:0];
        [returnButton setBackgroundImage:[UIImage imageNamed:@"middleNormal.png"] forState:0];
        [returnButton setTitle:@"完成" forState:0];
        [uv addSubview:returnButton];
        _ButtonComplete = returnButton;
		//[returnButton release];
		
		self.keyboardType = UIKeyboardTypeDecimalPad;
	}	
}

- (void) setNumberType
{
    //纯数字键盘
    _type = ENumberType;
    
    UIView* uv = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,JM_Return_Btn_Height)];
    self.inputView = nil;
	self.inputAccessoryView = uv;
    [uv release];
    
	UIButton *returnButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, JM_Return_Btn_Height)];
	[returnButton addTarget:self action:@selector(returnText:) forControlEvents:UIControlEventTouchUpInside];
    [returnButton setTitleColor:[UIColor blackColor] forState:0];
	[returnButton setBackgroundImage:[UIImage imageNamed:@"middleNormal.png"] forState:0];
	[returnButton setTitle:@"完成" forState:0];
    [uv addSubview:returnButton];
    _ButtonComplete = returnButton;
	//[returnButton release];
	
	self.keyboardType = UIKeyboardTypeNumberPad;
}

- (void) setPinYinType
{
    _type = EPinYinType;
    self.keyboardType = UIKeyboardTypeDefault;
    self.inputAccessoryView = nil;
}

- (void) returnText: (id)sender{
    [self resignFirstResponder];
}

- (void) nextClick: (id)sender
{
    [_nextEditer becomeFirstResponder];
}

-(void) setNextEditer:(JMTextField*) aNextEditer;
{
    _nextEditer = [aNextEditer retain];
    
    if ( (_type == EMoneyType && [[[UIDevice currentDevice] systemVersion] floatValue]>=4.1)||
         _type == ENumberType
        ) 
    {
        
        UIView* uv = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,JM_Return_Btn_Height)];
        self.inputView = nil;
        self.inputAccessoryView = uv;
        [uv release];
        
        
        UIButton *returnButton = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 160, JM_Return_Btn_Height)];
        [returnButton addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
        [returnButton setTitleColor:[UIColor blackColor] forState:0];
        [returnButton setBackgroundImage:[UIImage imageNamed:@"middleNormal.png"] forState:0];
        [returnButton setTitle:@"下一项" forState:0];
        [uv addSubview:returnButton];
        _ButtonNext = returnButton;
        //[returnButton release];
 
        UIButton *returnButton2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 160, JM_Return_Btn_Height)];
        [returnButton2 addTarget:self action:@selector(returnText:) forControlEvents:UIControlEventTouchUpInside];
        [returnButton2 setTitleColor:[UIColor blackColor] forState:0];
        [returnButton2 setBackgroundImage:[UIImage imageNamed:@"middleNormal.png"] forState:0];
        [returnButton2 setTitle:@"完成" forState:0];
        [uv addSubview:returnButton2];
        _ButtonNext = returnButton2;
        //[returnButton2 release];
    }
    
}

//JM 添加 用于数字键盘和通用键盘切换
-(void) setNumAndPinyin
{
    [self setNumberType];
    
    UIView* uv = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,JM_Return_Btn_Height)];
    self.inputView = nil;
    self.inputAccessoryView = uv;
    [uv release];
    
    
    UIButton *returnButton = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 160, JM_Return_Btn_Height)];
    [returnButton addTarget:self action:@selector(returnText:) forControlEvents:UIControlEventTouchUpInside];
    [returnButton setTitleColor:[UIColor blackColor] forState:0];
    [returnButton setBackgroundImage:[UIImage imageNamed:@"middleNormal.png"] forState:0];
    [returnButton setTitle:@"完成" forState:0];
    [uv addSubview:returnButton];
    _ButtonNext = returnButton;
    //[returnButton release];
    
    UIButton *returnButton2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 160, JM_Return_Btn_Height)];
    [returnButton2 addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
    [returnButton2 setTitleColor:[UIColor blackColor] forState:0];
    [returnButton2 setBackgroundImage:[UIImage imageNamed:@"middleNormal.png"] forState:0];
    [returnButton2 setTitle:@"切换" forState:0];
    [uv addSubview:returnButton2];
    _ButtonNext = returnButton2;
    //[returnButton2 release];

}
-(void) changeType:(UIButton*) aBtn
{
    [self resignFirstResponder];
    if (_type == ENumberType)
    {
        [self setPinYinType];
    }
    else
    {
        //纯数字键盘
        _type = ENumberType;
        
//        UIView* uv = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,JM_Return_Btn_Height)];
//        self.inputView = nil;
//        self.inputAccessoryView = uv;
//        [uv release];
//        
//        UIButton *returnButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 160, JM_Return_Btn_Height)];
//        [returnButton addTarget:self action:@selector(returnText:) forControlEvents:UIControlEventTouchUpInside];
//        [returnButton setTitleColor:[UIColor blackColor] forState:0];
//        [returnButton setBackgroundImage:[UIImage imageNamed:@"middleNormal.png"] forState:0];
//        [returnButton setTitle:@"完成" forState:0];
//        [uv addSubview:returnButton];
//        self._ButtonNext = returnButton;
//        [returnButton release];
//        
//        UIButton *returnButton2 = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 160, JM_Return_Btn_Height)];
//        [returnButton2 addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
//        [returnButton2 setTitleColor:[UIColor blackColor] forState:0];
//        [returnButton2 setBackgroundImage:[UIImage imageNamed:@"middleNormal.png"] forState:0];
//        [returnButton2 setTitle:@"切换" forState:0];
//        [uv addSubview:returnButton2];

        
        self.keyboardType = UIKeyboardTypeNumberPad;
    }
    [self becomeFirstResponder];
    self.text = self.text;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return self.canCopyAndPaste;
}

- (id)initWithFrame:(CGRect)frame WithList:(UITableView*) aTabelView WithIndex:(NSIndexPath*) aIndex
{
    return [self initWithFrame:frame WithList:aTabelView];
}
- (id)initWithType:(enum JMTextFieldType)aType WithList:(UITableView*) aTabelView WithIndex:(NSIndexPath*) aIndex
{
    return [self initWithType:aType WithList:aTabelView];
}
@end
